package com.tedu.udm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.udm.pojo.dto.TeamAddNewDTO;
import com.tedu.udm.pojo.dto.TeamUpdateDTO;
import com.tedu.udm.pojo.vo.TeamListItemVO;
import com.tedu.udm.pojo.vo.TeamStandardVO;
import com.tedu.udm.service.ITeamService;
import com.tedu.udm.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "98. 小组管理模块")
@Validated
@RestController
@RequestMapping("/teams")
public class TeamController {
    @Autowired
    private ITeamService teamService;
    public TeamController() {
        log.info("创建控制器对象：TeamController");
    }

    // http://localhost:9878/teams/add-new
    @ApiOperation("新增小组")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParam(name = "id", value = "班级id", required = true, dataType = "long")
    @PostMapping("/{classId:[0-9]+}/add-new")
    public JsonResult<Void> addNew(@Range(min = 1, message = "删除小组失败，尝试删除的小组的ID无效！")
                                       @PathVariable Long classId,TeamAddNewDTO teamAddNewDTO){
        log.debug("开始处理【添加小组】的请求，参数：{}", teamAddNewDTO);
        teamService.addNew(classId,teamAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/teams/9527/delete
    @ApiOperation("根据id删除小组")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "teamId", value = "小组id", required = true, dataType = "long")
    @PostMapping("/{teamId:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除小组失败，尝试删除的小组的ID无效！")
                                   @PathVariable Long teamId) {
        log.debug("开始处理【根据id删除小组】的请求，参数：{}", teamId);
        teamService.delete(teamId);
        return JsonResult.ok();
    }

    // http://localhost:9878/teams/9527/delete
    @ApiOperation("修改小组信息")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> update(TeamUpdateDTO teamUpdateDTO) {
        log.debug("开始处理【修改小组信息】的请求，参数：{}", teamUpdateDTO);
        teamService.update(teamUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/classes
    @ApiOperation("查询小组列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<TeamListItemVO>> list() {
        log.debug("开始处理【查询小组列表】的请求，无参数");
        List<TeamListItemVO> list = teamService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9878/classes
    @ApiOperation("根据班级id查询小组列表")
    @ApiOperationSupport(order = 401)
    @ApiImplicitParam(name = "classId", value = "班级id", required = true, dataType = "long")
    @GetMapping("/{classId:[0-9]+}/list-by-class")
    public JsonResult<List<TeamListItemVO>> listByClassId(@Range(min = 1, message = "查询小组失败，尝试查询的小组的ID无效！")
                                                               @PathVariable Long classId) {
        log.debug("开始处理【查询班级小组列表】的请求，参数：{}",classId);
        List<TeamListItemVO> list = teamService.listByClassId(classId);
        return JsonResult.ok(list);
    }

    // http://localhost:9878/classes
    @ApiOperation("查询小组详情")
    @ApiOperationSupport(order = 402)
    @GetMapping("/{teamId:[0-9]+}/get")
    public JsonResult<TeamStandardVO> listByTeamId(@Range(min = 1, message = "查询小组失败，尝试查询的小组的ID无效！")
                                                             @PathVariable Long teamId) {
        log.debug("开始处理【查询小组详情】的请求，参数：{}",teamId);
        TeamStandardVO team = teamService.get(teamId);
        return JsonResult.ok(team);
    }
}
